/* 战斗的工厂类 */
var ENUM_FIGHT_TYPE = {
    CLI: 0,
    DOUBLE: 1
}


var FightUnitMgr = function () {
    var self = this;
    self._btllist = [];
    self._enemylist = [];
    self._allunit = [];
    self._hashallunit = {};

    self.initByConfig = function () {
    }

    self.addUnit = function(funit){
        self._allunit.push(funit);
        self._hashallunit[funit["id"]] = funit;
    }

    self.getUnit = function(uid){
        return self._hashallunit[unit]
    }

    self.process = function(){
        for (var unit in self._allunit){
            self._allunit[unit].process();
        }
    }

    self.doDead = function(){  
    }

    self.doOnceKill = function(){
    }

    self.doMove = function(){
    }

    self.doHurt = function(unitid, hurtval){
    }
}

var FightDataMgr = function () {
    this._lvidx    = 0;         //关卡id
    this._lvtime   = 0;         //关卡持续时间
    this._startime = 0;         //战斗开始时间
    this._ntime    = 0;         //当前时间

    this._bulletdatas = []       //所有子弹数据
    this._enemydatas  = []       //所有敌人数据
    this._herodatas   = []       //所有的主角数据
    
    this._realtimedata = {       //当前的游戏状态
        isend:false,
        ntime:0,
        bulletcnt:0
    }      

    this.initByData = function (fdata) {
        this._lvtime = fdata["lvtime"];
        this._lvidx = fdata["lvidx"];
        this._startime = UtilTime.now();
        this._ntime  = this._startime;
    }

    this.addHero = function(funitdata){
        this._herodatas.push_back(funitdata);
    }

    this.addEnemy = function(funitdata){
        this._enemydatas.push_back(funitdata);
    }

    this.addBullet = function(funitdata){
        this._bulletdatas.push_back(funitdata);
    }

    this.process = function(dt){
        this._ntime += dt;
    }

    this.getRealTimeData = function () {
        this._realtimedata["isend"] = this.isTimeOut() || this.isBulletEmpty();
        this._realtimedata["bulletcnt"] = this._bulletdatas.length;
        this._realtimedata["ntime"] = this._ntime;
        return this._realtimedata;
    }
    
    this.isTimeOut = function () {
        //time
        if (UtilTime.now()>= this._startime + this._lvtime) return true;
    }

    this.isBulletEmpty = function(){
        this._bulletdatas.length == 0;
    }
}

var FightCliItem = function () {
    var self = this;
    //datamgr
    var datamgr = new FightDataMgr();
    //unitmgr 
    var unitmgr = new FightUnitMgr();

    self.startFight = function (fdata) {
        self.initFightData(fdata);
    }

    self.process = function (dt) {
        datamgr.process();
        unitmgr.process();
        // var isend = datamgr.isFightEnd();
        // if (isend) {
        //     self.endFight();
        //     return;
        // }
    }

    self.endFight = function () {
    }


    self.initFightData = function (fdata) {
        //data
        
        //unit
        var fdata = {}
        var funit = new FightUnit()
        funit.init();
        unitmgr.addUnit(funit);
    };
}

var FightFactory = (function () {
    var _public = {
        create: function (ftype) {
            //todo double 
            //现在暂时只返回客户端战斗
            return new FightCliItem()
        }
    }
    return _public;
})();
